const fs = require('fs')
const path = require('path')
let inquirer = require('inquirer');
let mkdirp = require('mkdirp');
var ejs = require("ejs");
const gulpLoadPlugins = require('gulp-load-plugins')

const plugins = gulpLoadPlugins()
/**
 * 根据用户输入创建页面
 */
class CreatedView {
    constructor() {
        this.Main();
    }

    async Main() {
        //问询输入
        let options = await this.Inquirer();
        // let options = {
        //     viewName: "aaa",
        //     viewPath: 'bbbb'
        // }
        options.pageSrc = `./pages/${options.viewPath}`
        options.jsSrc = `./static/modules/${options.viewPath}`
        //新建目录
        await this.Mkdirp(options.pageSrc)
        await this.Mkdirp(options.jsSrc)
        //根据配置创建相应文件到指定目录
        await this.CreatedFile(options);
    }

    /**
     * 根据配置创建相应文件到指定目录
     */
    async CreatedFile(options) {
        // render html
        let htmlStr = fs.readFileSync(__dirname + '/template/list/html.ejs', "utf8")
        htmlStr = ejs.render(htmlStr, {
            name: options.viewName,
            viewPath: options.viewPath,
            filename: __dirname
        })
        fs.writeFileSync(__dirname + `./../../${options.pageSrc}/${options.viewName}.html`, htmlStr)
        //render js
        let jsStr = fs.readFileSync(__dirname + '/template/list/js.ejs', "utf8")
        jsStr = ejs.render(jsStr, {
            name: options.viewName,
            viewPath: options.viewPath,
            filename: __dirname
        })
        fs.writeFileSync(__dirname + `./../../${options.jsSrc}/${options.viewName}.js`, jsStr)
        //添加router 
        let routerStr = fs.readFileSync('./static/js/wd-router.js', "utf8")
        fs.writeFileSync('./static/js/wd-router.js', routerStr.replace(`let router = {`, `let router = {\n    '${options.viewName}': './pages/${options.viewPath}/${options.viewName}.html',`))
        //添加左侧菜单
        let menuStr = `<li class="site-menu-item">
                                    <a class="wd-work-menu" data-href="/workshop?key=${options.viewName}">
                                        <span class="site-menu-title">${options.viewName}</span>
                                    </a>
                                </li>`
        let workshopHtml = fs.readFileSync('./workshop.html', "utf8");
        workshopHtml = workshopHtml.replace(/\<\!\-\- add-menu/, menuStr + '\n                                <!-- add-menu');
        fs.writeFileSync('./workshop.html', workshopHtml)
    }

    /**
     * 新建目录
     */
    async Mkdirp(path) {
        return new Promise((resolve, reject) => {
            mkdirp(path, (err) => {
                if (err) console.error(err)
                else resolve()
            })
        })
    }

    /**
     * 用户输入提示
     */
    async Inquirer() {
        return new Promise((resolve, reject) => {
            inquirer.prompt([{
                type: 'input',
                name: 'viewPath',
                message: '页面路径(必填):'
            }, {
                type: 'input',
                name: 'viewName',
                message: '创建页面的名称(必填):'
            }, {
                type: 'list',
                name: 'viewType',
                message: '选择页面类型',
                choices: ['listPage', 'editPage'],
                default: 'listPage'
            }]).then(options => {
                if (!options.viewPath) throw '页面路径必填';
                if (!options.viewName) throw '页面名称必填';
                resolve(options);
            }).catch(err => {
                throw new plugins.util.PluginError('InquirerError', err)
            })
        })
    }
}

var created = new CreatedView();

